package com.yaozheng.vocationaltraining.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yaozheng.vocationaltraining.R;
import com.yaozheng.vocationaltraining.bean.QuestionAnswerItemBean;
import com.yaozheng.vocationaltraining.bean.RewardBean;
import com.yaozheng.vocationaltraining.utils.BitmapLoader;
import com.yaozheng.vocationaltraining.widget.CircleImageView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by moon on 2017/11/10.
 */

public class RewardGridViewAdapter extends BaseAdapter {
    private LayoutInflater mInflater;//
    private Activity mContext;

    private List<RewardBean> data = new ArrayList<>();

    private int selectedPosition = -1;

    public RewardGridViewAdapter(Activity context,
                                  List<RewardBean> data) {
        this.mInflater = LayoutInflater.from(context);
        this.mContext = context;
        this.data = data;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(
                    R.layout.reward_grid_item, null);
            holder = new ViewHolder();

            holder.reward_textview = (TextView) convertView.
                    findViewById(R.id.reward_textview);

            convertView.setTag(holder);//
        } else {
            holder = (ViewHolder) convertView.getTag();//
        }

        holder.reward_textview.setText(data.get(position).getScore()+"");

        if(!data.get(position).isAvailable()){
            holder.reward_textview.setBackground(mContext.getResources().getDrawable(R.drawable.reward_enable_no_bg));
        }else {
            if (selectedPosition == position){
                holder.reward_textview.setTextColor(mContext.getResources().getColor(R.color.default_bg_color));
                holder.reward_textview.setBackground(mContext.getResources().getDrawable(R.drawable.reward_selected_bg));
            }else {
                holder.reward_textview.setTextColor(mContext.getResources().getColor(R.color.black));
                holder.reward_textview.setBackground(mContext.getResources().getDrawable(R.drawable.reward_un_selected_bg));
            }
        }

        return convertView;
    }

    public void setData(List<RewardBean> data) {
        this.data = data;
    }

    public void setselectedPosition(int selectedPosition) {
        if(data.get(selectedPosition).isAvailable()){
            this.selectedPosition = selectedPosition;
            notifyDataSetChanged();
        }
    }

    class ViewHolder {
        TextView reward_textview;
    }
}
